package com.atguigu.cloud.controller;/**
 * @Auter 李孝在
 * @Date 2024/11/18
 */

import com.atguigu.cloud.entities.*;
import com.atguigu.cloud.sevice.AdminService;
import com.atguigu.cloud.sevice.MerchantService;
import com.atguigu.cloud.sevice.UserService;
import com.atguigu.cloud.tool.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.controller
 * @className: RegisterControllor
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/18 20:45
 * @version: 1.0
 */

@RestController
@RequestMapping("/register")
public class RegisterController {
    @Resource
    private UserService userService;
    @Resource
    private MerchantService merchantService;
    @Resource
    private AdminService adminService;

    //小程序用户登录接口
    @PostMapping(value = "/wechat")
    @Operation(summary = "微信用户注册接口", description = "需要提供请求体提供认证对象,其中username和password的形参不可以变,必须提交username,password和phone,id可以为0,其他后端自动配置")
    public ResultData<Map<String,Integer>> registerFromWeChat(@RequestBody RegisterDTO registerDTO){
        if(!userService.onlyName(registerDTO.getUsername())){
            throw new RuntimeException("用户名已存在");
        }

        User user=new User(
                registerDTO.getId(),
                registerDTO.getUsername(),
                registerDTO.getPassword(),
                registerDTO.getPhone(),
                null,
                null

        );
        Map<String,Integer> map=new HashMap<>();
        map.put("id",userService.add(user));
        return ResultData.success(map);
    }

    //Web商户登录接口
    @PostMapping(value = "/WebMerchant")
    @Operation(summary = "Web商户注册接口",  description = "需要提供请求体提供认证对象,其中username和password的形参不可以变,必须提交username,password和phone,id可以为0,其他后端自动配置")
    public ResultData<Map<String,Integer>> registerFromWebMerchant(@RequestBody RegisterDTO registerDTO){

        if(!merchantService.onlyName(registerDTO.getUsername())){
            throw new RuntimeException("用户名已存在");
        }

        Merchant merchant=new Merchant(
            registerDTO.getId(),
            registerDTO.getUsername(),
            registerDTO.getPassword(),
            registerDTO.getPhone(),
            null,
            null
        );
        Map<String,Integer> map=new HashMap<>();
        map.put("id",merchantService.add(merchant));
        return ResultData.success(map);
    }

    //小程序用户登录接口
    @PostMapping(value = "/WebAdmin")
    @Operation(summary = "管理员注册接口",  description = "需要提供请求体提供认证对象,其中username和password的形参不可以变,必须提交username,password和phone,id可以为0,其他后端自动配置")
    public ResultData<Map<String,Integer>> registerFromAdmin(@RequestBody RegisterDTO registerDTO){

        if(!adminService.onlyName(registerDTO.getUsername())){
            throw new RuntimeException("用户名已存在");
        }

        Admin admin = new Admin(
            registerDTO.getId(),
            registerDTO.getUsername(),
            registerDTO.getPassword(),
            registerDTO.getPhone()
        );
        Map<String,Integer> map=new HashMap<>();
        map.put("id",adminService.add(admin));
        return ResultData.success(map);
    }

}
